<?php
namespace common\models\info;

use common\component\BaseActiveRecord;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\Url;

class Album extends BaseActiveRecord
{

    public static function tableName()
    {
        return '{{zt_album}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['created_at'], 'integer'],
            [['abstr','url'], 'string'],
            [['title'], 'string', 'max' => 255,"message"=>"标题超出指定长度"],
            ['title','required','message'=>'标题不能为空']
        ];
    }
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'abstr' => '简介',
            'created_at' => '创建时间',
            'url'=>'封面'
        ];

    }
    public function afterFind()
    {
        parent::afterFind();
    }


    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    self::EVENT_BEFORE_INSERT => ['created_at'],
                    self::EVENT_AFTER_UPDATE=>['created_at']
                ]
            ]
        ];
    }

    /**
     * 实例化模型对像
     * @param $id
     * @return BaseActiveRecord|null|static
     */
    public static function modelById($id) {
        if (!$id) {
            $model = new self();
        }
        else {
            $model = static::findOne($id);
        }
        return $model;
    }
    public static function dataListTable($post){
        //请求参数组装
        $where = ["and"];
        if (isset($post['title']) && !empty($post['title'])) {
            $where[] = ['like','title',trim($post['title'])];
        }
        if (isset($post['created_at'])) {
            $where[] = ['>=','created_at',strtotime($post['created_at'])];
        }
        $query = self::find()->where($where);
        $dataArr = self::table($query,$post);
        return $dataArr;
    }
    public static function getAllList($post,$page=1,$pageSize=10){
        $where = ["and"];
        if (isset($post['title']) && !empty($post['title'])) {
            $where[] = ['like','title',trim($post['title'])];
        }
        if (isset($post['created_at'])) {
            $where[] = ['>=','created_at',strtotime($post['created_at'])];
        }
        $q = static::find()->where($where)->orderBy("id DESC");
        $data = static::arrListByPage($q,$page,$pageSize);
        return $data;
    }
}